﻿/*
  Copyright (c) 2011, Mikheev Rostislav
  Copyright (c) 2011, FreshFarsh team

  Steel Engine source code is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
/**
  @author Mikheev Rostislav <hacenator@gmail.com>
  @date 23.03.2011
  @brief Runtime exception class
*/

#ifndef STEEL_EXCEPTION_H_
#define STEEL_EXCEPTION_H_

#include <stdexcept>
#include "common.h"
#include "basic_class.h"
#include "log.h"

namespace _STEEL_NAMESPACE {

/**
    @class jRuntimeError
    @author Rostislav Mikheev <hacenator@gmail.com>
    @brief Runtime exception
*/

class RuntimeError : public std::exception, steel::BasicClass {
 public :
  /**
   * Default constructor
   */
  RuntimeError() {};

  /**
   *
   */
  RuntimeError(const RuntimeError &rhs ) : msg (L"Runtime error: ")  {
    msg << rhs.msg.str();

    // converting
    char str[buffer_length];
    wcstombs(&str[0], msg.str().c_str(), buffer_length);
    _console->Error(str);
  }

  /**
   * Destructor
   */
  virtual ~RuntimeError() throw() {};

  /**
   * Return error message
   * @return
   * Text of error message
   */
  virtual const char *what() const throw()  {
    // converting
    char str[buffer_length];
    wcstombs(&str[0], msg.str().c_str(), buffer_length);
    return str;
  }

  /**
   * Error message accumulation
   */
  template<typename T>
    RuntimeError& operator<<( const T& t ) {
    msg << t;
    return *this;
  }

 private:
  static const int buffer_length = 1024;
  std::wstringstream msg; ///< error message
};


}

#endif // STEEL_EXCEPTION_H_
